import { getRandomOne } from "@/util/util";
// 装备各属性类型
import { Equipment, EquipEntry } from "./equipmentType";
// 装备词条池
import {
  entryPool,
  entryPoolList,
  prePool,
  prePoolList,
  sufPool,
  sufPoolList
} from "./entryPools/index";
// 装备池
import { equipPool, equipPoolList } from "./equipPools/index";
import {
  equipTypes,
  equipGradeName,
  equipTotalProps,
  EquipTotalPropsType
} from "./equipConst";
/**
  属性类型
  STR:力量
  VIT:体质
  DEX:敏捷
  HP:最大生命值
  RHP:生命缓和
  ATK:攻击力
  DEF:防御力
  AS:攻速
  CRR:暴击率
  CRD:暴击伤害

  百分比加成后面跟P 固定数值加成后面跟V 例如STRP STRV
*/

// 装备类型

const randomGrade = (arr: number[]) => {
  const firstArr = arr.map((m, index) => {
    return Array.from(new Array(m), () => index);
  });
  const secondArr = firstArr.reduce((a, b) => a.concat(b));
  return getRandomOne(secondArr) - 1;
};
// 随机制造装备
const randomGenerateEquip = (
  type?: string[],
  level?: number,
  grade = [20, 50, 20, 10, 5, 0, 1, 0]
) => {
  // 随机品级
  const gradeV: number = randomGrade(grade);
  if (gradeV === -1) {
    return null;
  }
  let list = Object.values(equipPool)[gradeV];
  if (list && list.length) {
    // 过滤装备类型
    if (type && type.length) {
      list = list.filter(m => type.includes(m.type));
    }
    // 过滤装备等级
    if (level) {
      list = list.filter(m => m.level <= level + 5 && m.level >= level - 2);
    }
    if (list && list.length) {
      return new Equipment(getRandomOne(list));
    }
  }
  return null;
};
// 批量造装备
const batchGenerateEquip = (options: any): Equipment[] => {
  let mount = options.mount || 1;
  const equips: Equipment[] = [];
  const type = options.type || [];
  const level = options.level || 5;
  const grade = options.grade || [20, 50, 20, 10, 5, 0, 1, 0];
  while (mount) {
    mount--;
    const equip = randomGenerateEquip(type, level, grade);
    if (equip) {
      equips.push(equip);
    }
  }
  return equips;
};
//  统计装备属性
const getEquipProps = (equip: Equipment): EquipTotalPropsType => {
  const { entry, pre, suf, props } = equip;
  const totalProps = JSON.parse(JSON.stringify(equipTotalProps));
  const pushToProps = (m: EquipEntry) => {
    const {
      type,
      isPercent,
      value
    }: { type: string; isPercent: boolean; value: number } = m;
    if (isPercent) {
      totalProps[type + "P"] += value;
    } else {
      totalProps[type] += value;
    }
  };
  entry.forEach(pushToProps);
  props.forEach(pushToProps);
  pre && pre.affProps.forEach(pushToProps);
  suf && suf.affProps.forEach(pushToProps);
  return totalProps;
};
export {
  entryPool,
  entryPoolList,
  prePool,
  prePoolList,
  sufPool,
  sufPoolList,
  equipPoolList,
  equipTypes,
  equipGradeName,
  randomGenerateEquip,
  batchGenerateEquip,
  getEquipProps
};
